package com.spdzen.controller.admin;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.model.NewsCategory;
import com.spdzen.service.NewsCategoryService;
import com.spdzen.support.AdminLoginUtils;
import com.spdzen.support.JsonUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.support.exception.ResultCodeException;
import com.spdzen.support.result.JsonResult;
import com.spdzen.support.validation.AddValidGroup;
import com.spdzen.support.validation.UpdateValidGroup;

@Controller
@RequestMapping("/admin/news/category")
public class NewsCategoryController {
	@Resource
	private NewsCategoryService newsCategoryService;

	@RequestMapping("/pageView.do")
	public String pageView() {
		return "/admin/news/newsCategoryPageView";
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonResult delete(Long id) {
		if(id == null || id.longValue() < 1) {
			return JsonResult.errorParameter();
		}
		
		try {
			return new JsonResult(newsCategoryService.deleteIfPermision(id));
		} catch (Exception e) {
			LogUtils.getLogger().error(id.toString(), e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/add.do")
	@ResponseBody
	public JsonResult add(@Validated(value = {AddValidGroup.class})NewsCategory record, BindingResult bindingResult) {
		if(bindingResult.hasErrors()){
			return JsonResult.errorParameter();
		}
		
		record.setOperateId(AdminLoginUtils.getUserId());
		record.setOperateTime(new Date());
		
		try {
			newsCategoryService.insertIfPermission(record);
			return JsonResult.success();
		} catch (ResultCodeException e) {
			return new JsonResult(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(record), e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonResult update(@Validated(value = {UpdateValidGroup.class})NewsCategory record, BindingResult bindingResult) {
		if(bindingResult.hasErrors()){
			return JsonResult.errorParameter();
		}
		
		record.setOperateId(AdminLoginUtils.getUserId());
		record.setOperateTime(new Date());

		try {
			newsCategoryService.updateIfPermission(record);
			return JsonResult.success();
		} catch (ResultCodeException e) {
			return new JsonResult(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(record), e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/queryAll.do")
	@ResponseBody
	public JsonResult queryAll() {
		try {
			return JsonResult.success(newsCategoryService.selectAdminVO());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/queryById.do")
	@ResponseBody
	public JsonResult queryById(Long id) {
		if(id == null || id.longValue() < 1) {
			return JsonResult.errorParameter();
		}
		
		try {
			return JsonResult.success(newsCategoryService.selectOneAdminVO(id));
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}
}
